<template>
  <div class="main-menu">
    <!-- logo -->
    <div class="logo">
      <img
        class="img"
        src="https://img1.baidu.com/it/u=3133679797,2917719189&fm=253&fmt=auto&app=120&f=JPEG"
        alt=""
      />
      <h2 v-show="!isCollapse" class="title">crud低调低调</h2>
    </div>
    <!-- menu -->
    <div class="menu">
      <el-menu active-text-color="#fff" :collapse="isCollapse">
        <template v-for="item in userMenus" :key="item.id">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <!-- 动态组件 -->
              <el-icon>
                <!-- el-icon-mataxxx -->
                <component :is="item.icon.split('-icon-')[1]"></component>
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item :index="subitem.id + ''" @click="handleItemClick(subitem)">{{
                subitem.name
              }}</el-menu-item>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import useLoginStore from '../../store/login/login'
import { mapPathToMenu } from '../../untils/map-menus'
// 定义props接收父组件的传值
defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})

// 从pinia中获取数据
const loginStore = useLoginStore()
const userMenus = loginStore.userMenus

// 监听item的点击
const router = useRouter()
function handleItemClick(item) {
  const url = item.url
  router.push(url)
}

// 默认菜单
const route = useRoute()
console.log(route.path, '--')
const pathMenu = mapPathToMenu(route.path, userMenus)
const defaultActive = computed(() => {
  return pathMenu.id + ''
})
</script>

<style scoped lang="less">
.main-menu {
  height: 100%;
  background-color: black;
}

.logo {
  display: flex;
  height: 28px;
  padding: 12px 10px 8px 10px;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;
  .img {
    height: 100%;
    margin: 0 10px;
  }
  .title {
    font-size: 16px;
    font-weight: 700;
    color: white;
    white-space: nowrap;
  }
}
.el-menu {
  border-right: none;
  user-select: none;
}
.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }
  .el-menu-item:hover {
    color: #fff;
  }
  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
